<?php

/*

Waiting for attacks (battle_waiting.php)

*/

include "global.inc.php";

$findTable = fetch("SELECT * FROM b2p_tables2 WHERE pet1_owner = '$userid' OR pet2_owner = '$userid' AND id = '$id' AND game = '$game'");
$findBattle = fetch("SELECT * FROM b2p_battles2 WHERE pet_id = '$findTable[pet1]' AND pet2_id = '$findTable[pet2]' AND game = '$game'");
$findBattle2 = fetch("SELECT * FROM b2p_battles2 WHERE pet_id = '$findTable[pet2]' AND pet2_id = '$findTable[pet1]' AND game = '$game'");

$findPet1 = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[pet_id]' AND game = '$game'");
$findPet2 = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[pet2_id]' AND game = '$game'");

if (($findBattle[pet_turn] == "1") AND ($findPet1[owner] == "$userid"))
{
	mysql_query("UPDATE b2p_battles2 SET pet_turn = '2' WHERE id = '$findBattle[id]' AND game = '$game'");
}

if (($findBattle2[pet_turn] == "1") AND ($findPet2[owner] == "$userid"))
{
	mysql_query("UPDATE b2p_battles2 SET pet_turn = '2' WHERE id = '$findBattle2[id]' AND game = '$game'");
}

$findBattle = fetch("SELECT * FROM b2p_battles2 WHERE id = '$findBattle[id]' AND game = '$game'");

if (($findBattle[pet_turn] == "2") AND ($findBattle2[pet_turn] >= "2") AND ($findBattle[pet_owner] == "$userid"))
{
	$damage1 = $findBattle2[damage] - $findBattle[denfese];
	if ($damage1 < 0) { $damage1 = 0; }

	mysql_query("UPDATE user_pets2 SET current_hp=$findPet1[current_hp]-$damage1 WHERE id = '$findBattle[pet1]' AND game = '$game'");

	$status_line = "$findBattle[status_line]<br>Overall: $findPet2[name] did $damage1 to $findPet1[name]";

	mysql_query("UPDATE b2p_battles2 SET status_line = '$status_line' WHERE id = '$findBattle[id]' AND game = '$game'");

	mysql_query("UPDATE b2p_battles2 SET damage = '0' WHERE id = '$findBattle[id]' AND game = '$game'");
	mysql_query("UPDATE b2p_battles2 SET defense = '0' WHERE id = '$findBattle[id]' AND game = '$game'");
	mysql_query("UPDATE b2p_battles2 SET pet_turn = '3' WHERE id = '$findBattle[id]' AND game = '$game'");
}

$findBattle = fetch("SELECT * FROM b2p_battles2 WHERE pet_id = '$findTable[pet1]' AND pet2_id = '$findTable[pet2]' AND game = '$game'");

if (($findBattle[pet_turn] >= "2") AND ($findBattle2[pet_turn] == "2") AND ($findBattle2[pet_owner] == "$userid"))
{
	$damage2 = $findBattle[damage] - $findBattle2[denfese];
	if ($damage2 < 0) { $damage2 = 0; }

	mysql_query("UPDATE user_pets2 SET current_hp=$findPet1[current_hp]-$damage2 WHERE id = '$findBattle[pet2]' AND game = '$game'");

	mysql_query("UPDATE b2p_battles2 SET damage = '0' WHERE id = '$findBattle2[id]' AND game = '$game'");
	mysql_query("UPDATE b2p_battles2 SET defense = '0' WHERE id = '$findBattle2[id]' AND game = '$game'");

	$status_line = "$findBattle[status_line]<br>Overall: $findPet1[name] did $damage2 to $findPet2[name]";

	mysql_query("UPDATE b2p_battles2 SET status_line = '$status_line' WHERE id = '$findBattle[id]' AND game = '$game'");
	mysql_query("UPDATE b2p_battles2 SET pet_turn = '3' WHERE id = '$findBattle2[id]' AND game = '$game'");
}

$findBattle = fetch("SELECT * FROM b2p_battles2 WHERE pet_id = '$findTable[pet1]' AND pet2_id = '$findTable[pet2]' AND game = '$game'");

$findPet1 = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[pet1]' AND game = '$game'");
$findPet2 = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[pet2]' AND game = '$game'");

if (($findBattle[pet_turn] == "3") AND ($findBattle2[pet_turn] == "3"))
{
	die("<p align=center>Both opponents are done with the round! <a href=battle_2p_fight.php?game=$game&id=$findBattle[id]>NEXT ROUND</a></p>");
}

ECHO <<<END
<meta http-equiv="refresh" content="20;URL=battle_waiting.php?game=$game&id=$id">
<p align=center>
Waiting for opponent to finish this round.<br>
This page will refresh every 20 seconds.<Br>
If it doesn't refresh, click <a href=battle_waiting.php?game=$game&id=$id>here</a>.
END;

?>